/* Note: this file is not used in favor of a plain text implementation inn spec.c. */

#include <stdio.h>
#include <stdlib.h>
#include <mxml.h>

#define MAXDRVFD 5

char    * welcome = NULL;
char    * distname = NULL;
char    * langset = NULL;
char    * kernelver = NULL;
char    * bootiso_fn = NULL;
char    * miimages_cddir = NULL;
unsigned  miinitrd_size = 0;

char    * hdpathes[] = {
    NULL,                       /* replaced with distname */
    "/boot",
    "/usr/share/MagicInstaller",
    "",  /* / */
    "/tmp"
};
unsigned  num_hdpathes = sizeof(hdpathes) / sizeof(hdpathes[0]);

char *cdpathes[] = {
    NULL                        /* replaced with miimages_cddir */
};
unsigned  num_cdpathes = sizeof(cdpathes) / sizeof(cdpathes[0]);

char *drvfd[MAXDRVFD];
unsigned  num_drvfd;

char *getnodetext(mxml_node_t *curnode)
{
    mxml_node_t *child_node;
    static char buf[255];
    int remain_len = sizeof(buf) - 1;

    buf[0] = '\0';
    for (child_node = curnode->child; child_node != NULL && remain_len > 0; child_node = child_node->next) {
        if (child_node->type == MXML_TEXT) {
            if (child_node->value.text.whitespace && remain_len > 0) {
                strncat(buf, " ", remain_len);
                remain_len--;
            }
            strncat(buf, child_node->value.text.string, remain_len);
            remain_len -= strlen(child_node->value.text.string);
        }
    }
    return buf;
}

int  parse_config_file(const char *filename) {
    FILE *fp;
    mxml_node_t * xmldoc;
    mxml_node_t * xmlroot;
    mxml_node_t  *curnode;
    mxml_node_t *child_node;
/*     mxml_node_t * probes; */
/*     mxml_node_t * default_pathspec; */

    /* Read the config in. */
    if ((fp = fopen(filename, "r")) == NULL) {
        fprintf(stderr, "Error read config file %s.\n", filename);
        return  0;
    }
    xmldoc = mxmlLoadFile(MXML_NO_CALLBACK, fp, MXML_NO_PARENT);
    if (xmldoc == NULL) {
        fprintf(stderr, "Error loading with mxmlLoadFile().\n");
        return 0;
    }

    fclose(fp);

    /* Search probes and default_pathspec.
       probes = NULL;  default_pathspec = NULL;
    */
    xmlroot = mxmlFindElement(xmldoc, xmldoc, "MagicBooter",
                              NULL, NULL, MXML_DESCEND_FIRST);
    if (xmlroot == NULL) {
        fprintf(stderr, "Error finding root with mxmlFindElement().\n");
        return 0;
    }
    for (curnode = xmlroot->child; curnode != NULL; curnode = curnode->next) {
        if (curnode->type == MXML_ELEMENT) {
#define GETTEXTVAL(var) \
            if (!strcmp(curnode->value.element.name, #var)) \
                var = strdup(getnodetext(curnode))
#define GETINTVAL(var) \
            if (!strcmp(curnode->value.element.name, #var)) \
                var = atoi(getnodetext(curnode))

            GETTEXTVAL(welcome);
            else GETTEXTVAL(distname);
            else GETTEXTVAL(langset);
            else GETTEXTVAL(kernelver);
            else GETTEXTVAL(bootiso_fn);
            else GETTEXTVAL(miimages_cddir);
            else GETINTVAL(miinitrd_size);
            else if (!strcmp(curnode->value.element.name, "drvfds")) {
                num_drvfd = 0;
                for (child_node = curnode->child; child_node != NULL; child_node = child_node->next) {
                    if (child_node->type == MXML_ELEMENT && !strcmp(child_node->value.element.name, "drvfd")) {
                        drvfd[num_drvfd++] = strdup(child_node->child->value.text.string);
                        if (num_drvfd >= MAXDRVFD)
                            break;
                    }
                }

            }
#undef GETTEXTVAL
#undef GETINTVAL
            /*
            if (!probes
                && !strcmp(curnode->value.element.name, "probes")) {
                probes = curnode;

                if (default_pathspec)  break;
            }
            if (!default_pathspec
                && !strcmp(curnode->value.element.name, "pathspec")
                && mxmlElementGetAttr(curnode, "name") == NULL) {
                default_pathspec = curnode;
                if (probes)  break;
            }
            */
        }
    }
    if (welcome == NULL
        || distname == NULL
        || langset == NULL
        || kernelver == NULL
        || bootiso_fn == NULL
        || miimages_cddir == NULL
        || miinitrd_size == 0) {
        fprintf(stderr, "Error reading config file value.\n");
        return 0;
    }
    hdpathes[0] = (char*)malloc(strlen(distname)+2);
    strcat(strcpy(hdpathes[0], "/"), distname);
    cdpathes[0] = (char*)malloc(strlen(miimages_cddir)+2);
    strcat(strcpy(cdpathes[0], "/"), miimages_cddir);
    printf("Size of miinitrd is %u\n", miinitrd_size);
#ifdef __SPEC_MAIN__
    printf("%s\n", welcome);
    printf("%s\n", distname);
    printf("%s\n", langset);
    printf("%s\n", kernelver);
    printf("%s\n", bootiso_fn);
    printf("%s\n", miimages_cddir);
    printf("%u\n", miinitrd_size);
    int i;
    printf("\nhdpathes:\n");
    for (i=0; i < num_hdpathes; i++)
        printf("\t%s\n", hdpathes[i]);
    printf("\ncdpathes:\n");
    for (i=0; i < num_cdpathes; i++)
        printf("\t%s\n", cdpathes[i]);
    printf("\ndrvfds:\n");
    for (i=0; i < num_drvfd; i++)
        printf("\t%s\n", drvfd[i]);
#endif
    return  1;
}

#ifdef __SPEC_MAIN__
int main(int argc, char *argv[])
{
    printf("%s\n", (parse_config_file("../etc/mbconfig.xml") ? "OK" : "BAD"));
    return 0;

}
#endif
